﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using DBAccessLibrary.Model;
using QueryResource.Common;
using QueryResource.ViewModel;

namespace BaseControlLibrary
{
    /// <summary>
    /// DBTreeViewControl.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class DBTreeViewControl 
    {
        public DBTreeViewControl()
        {
            InitializeComponent();
            ViewModel = new DBTreeViewModel();


            this.dbtreeview.AddHandler(MouseWheelEvent, new RoutedEventHandler(dbtreeview_MouseWheel), true);
            this.dbtreeview.AddHandler(PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(dbtreeview_MouseLeftButtonDown), true);
            this.dbtreeview.AddHandler(PreviewMouseLeftButtonUpEvent, new RoutedEventHandler(dbtreeview_MouseLeftButtonUp), true);
            this.dbtreeview.AddHandler(MouseLeaveEvent, new RoutedEventHandler(dbtreeview_MouseLeftButtonUp), true);
            this.dbtreeview.MouseMove += dbtreeview_MouseMove;

            this.dbtreeview.PreviewMouseMove += dbtreeview_PreviewMouseMove;
        }

        //拖拽
        void dbtreeview_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (dbtreeview.SelectedItem != null)
                {
                    DataBaseEntity entity = dbtreeview.SelectedItem as DataBaseEntity;
                    if (entity != null)
                    {
                        if (entity is TableEntity || entity is FieldEntity || entity is ViewEntity)
                        {
                            DragDrop.DoDragDrop(this.dbtreeview, entity.TransName, DragDropEffects.Move);
                        }
                    }
                }
                   
              
            }
        }

        public DBTreeViewModel ViewModel { get; set; }
        public void Init()
        {

            ViewModel.TreeViewBegionInvoke = fun => this.dbtreeview.BeginInvokeEx(fun);
            ViewModel.LoadData();
            this.dbtreeview.DataContext = ViewModel;

        }

        /// <summary>
        /// 右键 单击 默认选择节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbtreeviewPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = GetTemplatedAncestor<TreeViewItem>(e.OriginalSource as FrameworkElement);
            if (item != null)
            {
                item.IsSelected = true;
            }
        }
        private T GetTemplatedAncestor<T>(FrameworkElement element) where T : FrameworkElement
        {
            if (element is T)
            {
                return element as T;
            }

            FrameworkElement templatedParent = element.TemplatedParent as FrameworkElement;
            if (templatedParent != null)
            {
                return GetTemplatedAncestor<T>(templatedParent);
            }

            return null;
        }

        /// <summary>
        /// 选择节点 触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbtreeviewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            dbtreeview.Focus();
            ViewModel.SelectedItem = dbtreeview.SelectedItem as DataBaseEntity;
        }


        #region 滑动
        private Point lastPoin;
        private bool isMove ;

        void dbtreeview_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            if (isMove)
            {
                isMove = false;
                lastPoin = new Point(0, 0);
            }
        }

        void dbtreeview_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMove)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    if (lastPoin.X != 0 && lastPoin.Y != 0)
                    {
                        Point p = e.GetPosition(this.dbtreeview);

                        this.sv.ScrollToHorizontalOffset(sv.HorizontalOffset + (lastPoin.X - p.X));
                        this.sv.ScrollToVerticalOffset(sv.VerticalOffset + (lastPoin.Y - p.Y));
                    }
                    else
                    {
                        lastPoin = e.GetPosition(this.dbtreeview);
                    }
                }
                e.Handled = true;
            }
        }
        void dbtreeview_MouseWheel(object sender, RoutedEventArgs e)
        {
            MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;

            double x = eargs.Delta;

            double y = sv.VerticalOffset;

            sv.ScrollToVerticalOffset(y - x);
        }
        void dbtreeview_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            if (sv.ComputedHorizontalScrollBarVisibility != System.Windows.Visibility.Collapsed ||
                sv.ComputedVerticalScrollBarVisibility != System.Windows.Visibility.Collapsed)
            {
                
                MouseButtonEventArgs me = (MouseButtonEventArgs) e;
                if (me.LeftButton == MouseButtonState.Pressed)
                {
                    isMove = true;
                }
            }
        }
        
        #endregion

      
    }
}
